package dictionary

import (
    "github.com/gin-gonic/gin"
    "github.com/jinzhu/copier"
    "iip/dictionary/model"
)

func (s *Dictionary) TruckCreate(c *gin.Context) {
    req := &struct {
        LicensePlate  string `form:"license_plate"   json:"license_plate"   binding:"required"`
        TruckType     string `form:"truck_type"      json:"truck_type"      binding:"required"`
        VIN           string `form:"vin"             json:"vin"             binding:"required"`
        WeightUnladen int    `form:"weight_unladen"  json:"weight_unladen"  binding:"required"`
        WeightMax     int    `form:"weight_max"      json:"weight_max"      binding:"required"`
        CompanyID     int64  `form:"company_id"      json:"company_id"      binding:"required"`
        Purpose       string `form:"purpose"         json:"purpose"         binding:"required"`
        PhotoID       string `form:"photo_id"        json:"photo_id"        binding:""`
        Appearance    string `form:"appearance"      json:"appearance"      binding:"required"`
    }{}
    if err := c.ShouldBind(req); err != nil {
        s.logger.Error(err)
        s.json_error(c, err.Error())
        return
    }
    s.logger.Info(req, req.CompanyID)


    com, err := s.GetCompanyByID(req.CompanyID)
    if err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    } else {
        s.logger.Infof("find company[%d]=%s", com.ID, com.Name)
    }


    row := model.DictTruck{}
    _ = copier.Copy(&row, req)
    row.ID = s.idgen.NextID()
    if err := s.gorm().Create(&row).Error; err != nil {
        s.json_error(c, err.Error())
        return
    }
    s.logger.Info("create truck done")
    s.json_data(c, s.truck_full(&row, com))
}

func (s *Dictionary) TruckDelete(c *gin.Context) {
    req := &struct {
        ID int64 `uri:"id"`
    }{}
    if err := c.ShouldBindUri(req); err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }

    if err := s.gorm().Delete(&model.DictTruck{ID: req.ID}).Error; err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }
    s.logger.Info("delete truck by id ", req.ID)
    s.json_ok(c)
}

func (s *Dictionary) TruckPagedList(c *gin.Context) {

    query := &struct {
        Page     int    `form:"page" binding:"required,gte=1" json:"page"`
        PageSize int    `form:"pagesize" binding:"required,gte=1" json:"pagesize"`
        Order    string `form:"order" json:"order"`
    }{}
    if err := c.ShouldBindQuery(query); err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }
    if query.Order != "asc" && query.Order != "desc" {
        query.Order = "asc"
    }


    var total int64
    if err := s.gorm().Model(&model.DictTruck{}).Count(&total).Error; err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }

    var com_lst []*model.DictTruck
    if err := s.gorm().Limit(query.PageSize).Offset(query.PageSize * (query.Page - 1)).Order("id " + query.Order).Find(&com_lst).Error; err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }

    var lst []interface{}
    for _, com := range com_lst {
        loc, _ := s.GetCompanyByID(com.CompanyID)
        lst = append(lst, s.truck_full(com, loc))
    }
    s.logger.Info("query truck paged list")
    s.json_paged_data(c, total, query.Page, query.PageSize, lst)
}

func (s *Dictionary) TruckByID(c *gin.Context) {
    req := &struct {
        ID int64 `uri:"id"`
    }{}
    if err := c.ShouldBindUri(req); err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }

    row, err := s.GetTruckByID(req.ID)
    if err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }
    com, err := s.GetCompanyByID(row.CompanyID)
    if err != nil {
        s.logger.Error(err)
    }

    s.logger.Info("query truck by id ", req.ID)
    s.json_data(c, s.truck_full(row, com))
}

func (s *Dictionary) TruckUpdate(c *gin.Context) {

    req_id := &struct {
        ID int64 `uri:"id"`
    }{}
    if err := c.ShouldBindUri(req_id); err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }
    drv, err := s.GetTruckByID(req_id.ID)
    if err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }


    var row model.DictTruck
    if err := c.BindJSON(&row); err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    }
    row.ID = drv.ID
    row.CreatedAt = drv.CreatedAt
    row.UpdatedAt = drv.UpdatedAt
    row.DeletedAt = drv.DeletedAt


    com, err := s.GetCompanyByID(row.CompanyID)
    if err != nil {
        s.logger.Error(err.Error())
    }

    if err := s.db.Save(row).Error; err != nil {
        s.logger.Error(err.Error())
        s.json_error(c, err.Error())
        return
    } else {
        s.logger.Info("update truck id=", row.ID)
        s.json_data(c, s.truck_full(&row, com))
    }
}
